from django.db import models
from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer as TJS

# Create your models here.
from django.db import models
from meiduo_mall.utils.models import BaseModel

class OAuthQQUser(BaseModel):
    """QQ登陆用户数据"""
    # on_delete=models.CASCADE 级联删除
    user = models.ForeignKey('user.User', on_delete=models.CASCADE, verbose_name='用户')
    # 用openid做索引
    openid = models.CharField(max_length=64, verbose_name='openid', db_index=True)

    class Meta:
        db_table = 'tb_oauth_qq'
        verbose_name = 'QQ登陆用户数据'
        verbose_name_plural = verbose_name


    @staticmethod
    def decode_token(access_token):
        tjs = TJS(settings.SECRET_KEY, 300)

        try:
            data = tjs.loads(access_token)
        except:
            raise Exception('错误的access_token')

        # 获取openid数据
        openid = data.get('openid')
        if not openid:
            raise Exception('access_token　失效')

        return openid

